package com.ctibook.booksystem.service.bookcatalog.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.ctibook.booksystem.dao.bookcatalog.BookInfoUploadFileRecordDao;
import com.ctibook.booksystem.model.bookcatalog.BookInfoUploadFileRecord;
import com.ctibook.booksystem.service.bookcatalog.BookInfoUploadFileRecordService;
import com.ctibook.booksystem.vo.bookcatalog.BookInfoUploadFileRecordVO;


@Service("bookInfoUploadFileRecordService")
public class BookInfoUploadFileRecordServiceImpl implements BookInfoUploadFileRecordService {
	
	
	@Resource
	private BookInfoUploadFileRecordDao bookInfoUploadFileRecordDao;
	
	
	@Override
	public void addBookInfoUploadFileRecord(String fileName, String creatorId,String creatorName,Boolean isCheckedIsbn) {
		BookInfoUploadFileRecord record=new BookInfoUploadFileRecord();
		record.setFileName(fileName);
		record.setFileType(".zip");
		record.setCheckedIsbn(isCheckedIsbn);
		record.setCreatorId(creatorId);
		record.setCreatorName(creatorName);
		bookInfoUploadFileRecordDao.addUploadRecord(record);
	}

	@Override
	public void updateBookInfoUploadFileRecord(BookInfoUploadFileRecordVO vo) {
		if(StringUtils.isNotBlank(vo.getId())){
			BookInfoUploadFileRecord record=(BookInfoUploadFileRecord) bookInfoUploadFileRecordDao.getUploadFileRecordById(vo.getId());
			if(record!=null){
				BeanUtils.copyProperties(vo, record,"id","creator","createTime");
				bookInfoUploadFileRecordDao.updateUploadFileRecord(record);
			}
		}
	}

	@Override
	public void deleteBookInfoUploadFileRecord(String id) {
		BookInfoUploadFileRecord record=(BookInfoUploadFileRecord) bookInfoUploadFileRecordDao.getUploadFileRecordById(id);
		if(record!=null){
			bookInfoUploadFileRecordDao.deleteUploadRecord(record);
		}
	}

	@Override
	public List<BookInfoUploadFileRecordVO> getRequireHandlerBookInfoUploadFileRecordVos() {
		List<BookInfoUploadFileRecord> records = bookInfoUploadFileRecordDao.getRequireHandlerBookInfoUploadRecords();
		
		return convertToVos(records);
	}

	private List<BookInfoUploadFileRecordVO> convertToVos(List<BookInfoUploadFileRecord> records) {
		List<BookInfoUploadFileRecordVO> vos=new ArrayList<BookInfoUploadFileRecordVO>();
		if(records!=null&&records.size()>0){
			for (BookInfoUploadFileRecord record : records) {
				BookInfoUploadFileRecordVO vo=new BookInfoUploadFileRecordVO(record);
				vos.add(vo);
			}
		}
		return vos;
	}

	@Override
	public List<BookInfoUploadFileRecordVO> getBookInfoUploadFileRecordVO(
	    String startDate, String endDate, String parsered, String operaterId) {
		List<BookInfoUploadFileRecordVO> vos=new ArrayList<BookInfoUploadFileRecordVO>();
		List<BookInfoUploadFileRecord>  fileRecords=bookInfoUploadFileRecordDao.getBookInfoUploadFileRecord(startDate,endDate,
				parsered,operaterId);
		if(fileRecords!=null && fileRecords.size()>0){
			for (BookInfoUploadFileRecord record : fileRecords) {
				BookInfoUploadFileRecordVO vo=new BookInfoUploadFileRecordVO(record);
				vos.add(vo);
			}
		}
		return vos;
	}
	
	
}
